package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime;

/**
 * 学生统计信息实体类
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString(exclude = {"student", "term"})
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "student_statistics", indexes = {
        @Index(name = "idx_student_stats_student", columnList = "student_id"),
        @Index(name = "idx_student_stats_term", columnList = "term_id"),
        @Index(name = "idx_student_stats_score", columnList = "average_score")
})
public class StudentStatisticsEntity {

    /**
     * 统计ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 学生ID
     */
    @NotNull(message = "学生ID不能为空")
    @Column(name = "student_id", nullable = false)
    private Long studentId;

    /**
     * 学期ID
     */
    @NotNull(message = "学期ID不能为空")
    @Column(name = "term_id", nullable = false)
    private Long termId;

    /**
     * 平均分数 (0-100)
     */
    @NotNull(message = "平均分数不能为空")
    @DecimalMin(value = "0.0", message = "平均分数不能小于0")
    @DecimalMax(value = "100.0", message = "平均分数不能大于100")
    @Column(name = "average_score", nullable = false)
    private Double averageScore;

    /**
     * 出勤率 (0-100)
     */
    @NotNull(message = "出勤率不能为空")
    @DecimalMin(value = "0.0", message = "出勤率不能小于0")
    @DecimalMax(value = "100.0", message = "出勤率不能大于100")
    @Column(name = "attendance_rate", nullable = false)
    private Double attendanceRate;
    /**
     * 荣誉评价
     */
    @NotBlank(message = "荣誉评价不能为空")
    @Size(max = 20, message = "荣誉评价长度不能超过20个字符")
    @Column(name = "honor_evaluation", nullable = false, length = 20)
    private String honorEvaluation;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 学生信息
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "student_id", insertable = false, updatable = false)
    private UserEntity student;

    /**
     * 学期信息
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "term_id", insertable = false, updatable = false)
    private TermEntity term;

    /**
     * 获取成绩等级
     */
    public String getGradeLevel() {
        if (averageScore == null) return "未知";
        if (averageScore >= 90) return "优秀";
        if (averageScore >= 80) return "良好";
        if (averageScore >= 70) return "中等";
        if (averageScore >= 60) return "及格";
        return "不及格";
    }

    /**
     * 验证统计数据是否有效
     */
    public boolean isValid() {
        return averageScore != null && attendanceRate != null &&
                averageScore >= 0 && averageScore <= 100 &&
                attendanceRate >= 0 && attendanceRate <= 100;
    }
}
